package com.inspur.edp.bef.bemanager.codegenerator.actions;


import com.inspur.edp.bef.bemanager.codegenerator.JavaCompCodeNames;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.bizentity.operation.BizOperation;

public class JavaBeActionGenerator extends JavaBaseActionGenerator {
    private boolean isRoot;

    ///#region 字段和属性
    @Override
    protected String getBaseClassName() {
        if (isRoot) {
            return "RootAbstractAction<" + ReturnTypeName + ">";
        }
        return "AbstractAction<" + ReturnTypeName + ">";
    }


    ///#endregion


    ///#region 构造函数
    public JavaBeActionGenerator(GspBusinessEntity be, BizOperation operation, String nameSpace, String path) {
        super(be, operation, nameSpace, path);
        if (be.getMainObject().getCode().equals(operation.getOwner().getCode())) {
            isRoot = true;
        } else {
            isRoot = false;
        }
        //  this.isRoot = isRoot;
    }

    @Override
    protected String GetNameSpaceSuffix() {
        return BizOperation.getOwner().getCode() + "." + JavaCompCodeNames.ActionNameSpaceSuffix;
    }


    ///#endregion


    ///#region GenerateConstructor

    @Override
    protected void JavaGenerateConstructor(StringBuilder result) {
        result.append(GetIndentationStr()).append(JavaCompCodeNames.KeywordPublic).append(" ").append(GetCompName()).append("(");
        //result.append(GetDoubleIndentationStr()).append(GetIndentationStr()).append("IBENodeEntityContext beContext");
        result.append("IBENodeEntityContext beContext");

        if (JavaHasCustomConstructorParams()) {
            //result.appendLine(",");
            result.append(",");
            JavaGenerateConstructorParams(result);
        } else {
            result.append("");
        }

        //result.append(GetDoubleIndentationStr()).append(GetIndentationStr()).appendLine(") : base(beContext)");
        result.append(")");

        result.append(" {").append(getNewline());
        result.append(GetIndentationStr()).append(GetIndentationStr()).append("super(beContext);").append(getNewline()); //新加项
        JavaGenerateConstructorContent(result);
        result.append(GetIndentationStr()).append("}").append(getNewline()).append(getNewline());
    }

    @Override
    protected void JavaGenerateExtendUsing(StringBuilder result) {
        super.JavaGenerateExtendUsing(result);
        result.append(GetImportStr(JavaCompCodeNames.AbstractActionNameSpace)).append(GetImportStr(JavaCompCodeNames.IEntityDataNameSpace));
    }


    ///#endregion


    ///#region GenerateExtendMethod
    @Override
    protected void JavaGenerateExtendMethod(StringBuilder result) {
        if (this.isInterpretation) {
            JavaInterpretationBeActionExtendMethod(result);
        } else {
            JavaBeActionExtendMethod(result);
        }
    }

    ///#endregion

    protected void JavaBeActionExtendMethod(StringBuilder result) {
        result.append(GetIndentationStr()).append(JavaCompCodeNames.KeywordPrivate).append(" ").append(EntityClassName).append(" getData() ");
        result.append("{").append(getNewline());

        result.append(GetIndentationStr()).append(GetIndentationStr()).append("return ").append("(").append(EntityClassName).append(")").append("getBEContext().getCurrentData()").append(";").append(getNewline());

        result.append(GetIndentationStr()).append("}").append(getNewline());
    }

    protected void JavaInterpretationBeActionExtendMethod(StringBuilder result) {
        result.append(GetIndentationStr()).append(JavaCompCodeNames.KeywordPrivate).append(" ").append(JavaCompCodeNames.IEntityData).append(" getData() ");
        result.append("{").append(getNewline());

        result.append(GetIndentationStr()).append(GetIndentationStr()).append("return ").append("(").append(JavaCompCodeNames.IEntityData).append(")").append("getBEContext().getCurrentData()").append(";").append(getNewline());

        result.append(GetIndentationStr()).append("}");
    }

    @Override
    protected String GetInitializeCompName() {
        return String.format("%1$s%2$s%3$s", getChildCode(), BizOperation.getCode(), "Action");
    }
}